package com.study.system.user.controller;


import com.alibaba.fastjson.JSON;
import com.study.system.BaseServlet;
import com.study.system.user.domain.LoginUser;
import com.study.system.user.domain.User;
import com.study.system.user.service.UserService;
import com.study.system.user.service.impl.UserServiceImpl;
import com.study.system.utils.CheckCodeUtil;
import com.study.system.utils.DataUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @Author 669
 * @CreateDate 2022/4/19 19:01
 * @Version 1.0
 */
@WebServlet("/register/*")
public class RegisterController extends BaseServlet {

    //验证码校验
    public void checkCodeServlet(HttpServletRequest req, HttpServletResponse resp) {
        // 生成验证码
        ServletOutputStream os = null;
        String checkCode = null;
        try {
            os = resp.getOutputStream();
            checkCode = CheckCodeUtil.outputVerifyImage(100, 50, os, 4);
            // 存入Session
            HttpSession session = req.getSession();
            session.setAttribute("checkCodeGen", checkCode);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //判断用户名是否合法
    public void checkUsernameServlet(HttpServletRequest req, HttpServletResponse resp) {
        try {
            //获取登录信息
            String username = req.getParameter("username");
            System.out.println(username);
            //查询用户是否存在
            UserService service = new UserServiceImpl();
            User user = service.selectByUsername(username);
            //用户存在响应为ture
            if (user != null) {
                resp.getWriter().write("true");
            } else {
                //用户不存在响应为0
                resp.getWriter().write("false");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //注册
    public void registerUser(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String name = req.getParameter("name");
            String dept = req.getParameter("dept");

            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setName(name);
            user.setDate(DataUtils.getDate());
            user.setDeptid(Integer.parseInt(dept));

            //添加用户
            UserService service = new UserServiceImpl();
            int i = service.addUser(user);
            if (i == 1) {
                req.setAttribute("login_msg2", "注册成功！");
                //转发请求
                req.getRequestDispatcher("/jsp/login.jsp").forward(req, resp);
            } else {
                req.setAttribute("register_msg", "注册失败!");
                req.getRequestDispatcher("/jsp/register.jsp").forward(req, resp);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

